<?php
namespace Home\Controller;
use Think\Controller;
use Org\Util\Rbac;
// 角色模块
class RoleController extends CommonController {

    function _filter(&$map){
        if(!empty($_POST['name'])) {
            $map['title'] = array('like',"%".$_POST['name']."%");
        }
    }

    public function accessList(){
        $model = D('Role');
        $groupId = I('id');
        $group = $model->find($groupId);
        $apps = $model->getGroupAppList($groupId);
        $moduleList = $model->getGroupModuleList($groupId, $apps[0]['id']);
        foreach ($moduleList as $key => $module) {
            $result[$module['id']] = $module;
            $result[$module['id']]['actions'] = $model->getModuleActionList($module['id']);
        }
        $accessList = Rbac::getAccessList($_SESSION[C('USER_AUTH_KEY')]);
        if($accessList){
            $roles = array();
            foreach ($accessList[strtoupper(APP_NAME)] as $module => $actions) {
                $roles+= array_values($actions);
            }
            $this->assign('roles', implode(',', $roles));
        }else{
            $this->assign('roles', '');
        }
        $this->assign('appid', $apps[0]['id']);
        $this->assign('group', $group);
        $this->assign('result', $result);
        $this->display();
    }

    public function saveAccessList(){
        $groupId = I('groupID');
        $model = D('Role');
        $apps = $model->getGroupAppList($groupId);
        $moduleList = $model->getGroupModuleList($groupId, $apps[0]['id']);
        foreach ($moduleList as $key => $module) {
            $model->delGroupAction($groupId, $module['id']);
        }
        $res = $model->setGroupModules($groupId, I('rule'));
        if($res)
            $this->success('更新成功');
        else
            $this->error('更新失败');
    }

    public function user() {
        //读取系统的用户列表
        $user = D("User");
        $list2 = $user->field('id,account,nickname')->select();
        $group = D("Role");

        //获取当前用户组信息
        $groupId = isset($_GET['id'])?$_GET['id']:'';
        $groupUserList = array();
        if(!empty($groupId)) {
            $this->assign("group", $group->find($groupId));
            //获取当前组的用户列表
            $list = $group->getGroupUserList($groupId);
            $list2_indexed = array_column($list2, null, 'id');
            foreach ($list as $vo){
                if(isset($list2_indexed[$vo['id']]))
                    $groupUserList[] = $list2_indexed[$vo['id']];
            }
        }else{
            $this->error('群组id参数不对');
        }
        $this->assign('groupUserList',$groupUserList);
        $this->display();
    }

    public function _before_edit(){
       $Group = D('Role');
        //查找满足条件的列表数据
        $list = $Group->field('id,name')->select();
        $this->assign('list',$list);

    }

    public function _before_add(){
       $Group = D('Role');
        //查找满足条件的列表数据
        $list = $Group->field('id,name')->select();
        $this->assign('list',$list);

    }
}